//
//  CustomDatePickerView.swift
//  UI Single
//
//  Created by vivi on 6/3/16.
//  Copyright © 2016 vivi. All rights reserved.
//

import UIKit
import SnapKit

class CustomDatePickerBaseView: UIView, DatePickerTableViewDelegate {
    
    var layoutView1: UIView! //占位使用的UIView
    var layoutView2: UIView! //占位使用的UIView
    var tableContainerView1: UIView! //用来布局table view的
    var tableContainerView2: UIView! //用来布局table view的
    var tableContainerView3: UIView! //用来布局table view额
    var tableView1: DatePickerTableView!
    var tableView2: DatePickerTableView!
    var tableView3: DatePickerTableView!
    var commitButton: UIButton!
    var topMaskView: CustomDatePickerMaskView!
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configureInit()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configureInit()
    }
    
    func configureInit() {
        self.clipsToBounds = true
        
        //布局layoutView
        layoutView1 = UIView()
        layoutView1.backgroundColor = UIColor.whiteColor()
        self.addSubview(layoutView1)
        layoutView1.snp_makeConstraints {
            make in
            make.left.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(self).multipliedBy(0.175)
        }
        
        layoutView2 = UIView()
        layoutView2.backgroundColor = UIColor.whiteColor()
        self.addSubview(layoutView2)
        layoutView2.snp_makeConstraints {
            make in
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(self).multipliedBy(0.233)
        }
        
        tableContainerView1 = UIView()
        tableContainerView1.clipsToBounds = true
        tableContainerView1.backgroundColor = UIColor.blueColor()
        self.addSubview(tableContainerView1)
        tableContainerView1.snp_makeConstraints {
            make in
            make.left.equalTo(layoutView1.snp_right)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(self).multipliedBy(0.167)
        }
        
        tableContainerView3 = UIView()
        tableContainerView3.clipsToBounds = true
        tableContainerView3.backgroundColor = UIColor.greenColor()
        self.addSubview(tableContainerView3)
        tableContainerView3.snp_makeConstraints {
            make in
            make.width.equalTo(self).multipliedBy(0.261) 
            make.right.equalTo(layoutView2.snp_left)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
        }
        
        tableContainerView2 = UIView()
        tableContainerView2.clipsToBounds = true
        tableContainerView2.backgroundColor = UIColor.yellowColor()
        self.addSubview(tableContainerView2)
        tableContainerView2.snp_makeConstraints {
            make in
            make.left.equalTo(tableContainerView1.snp_right)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.right.equalTo(tableContainerView3.snp_left)
        }
        
        //布局tableView
        tableView1 = DatePickerTableView()
        tableContainerView1.addSubview(tableView1)
        tableView1.snp_makeConstraints { make in make.edges.equalTo(tableContainerView1) }
        
        tableView2 = DatePickerTableView()
        tableContainerView2.addSubview(tableView2)
        tableView2.snp_makeConstraints { make in make.edges.equalTo(tableContainerView2) }
        
        tableView3 = DatePickerTableView()
        tableContainerView3.addSubview(tableView3)
        tableView3.snp_makeConstraints { make in make.edges.equalTo(tableContainerView3) }
        
        tableView1.datePickerDelegate = self
        tableView2.datePickerDelegate = self
        tableView3.datePickerDelegate = self
        
        tableView1.tag = 0
        tableView2.tag = 1
        tableView3.tag = 2
        
        //布局commit button
        commitButton = UIButton(type: .Custom)
        //commitButton.backgroundColor = UIColor.blueColor()
        commitButton.setBackgroundImage(UIImage(named: "圈勾"), forState: .Normal)
        commitButton.addTarget(self, action: nil, forControlEvents: .TouchUpInside)
        self.addSubview(commitButton)
        commitButton.snp_makeConstraints {
            make in
            make.height.equalTo(23.667)
            make.width.equalTo(23.667)
            make.centerY.equalTo(self)
            make.centerX.equalTo(self).multipliedBy(1.738)
        }
        
//        let gradientImageView = UIImageView(image: UIImage(named: "GradientGray"))
//        gradientImageView.contentMode = .ScaleAspectFill
//        self.addSubview(gradientImageView)
//        gradientImageView.snp_makeConstraints { make in make.edges.equalTo(self) }
        
        //布局mask view
        topMaskView = CustomDatePickerMaskView()
//        topMaskView.backgroundColor = UIColor.blackColor()
//        topMaskView.alpha = 0.03
        self.addSubview(topMaskView)
        topMaskView.snp_makeConstraints { make in make.edges.equalTo(self)}
    }
    
    //MARK: - Handler
    func commitButtonHandler() {
        
    }
    
    //MARK: - Delegate
    func datePickerTableViewDidSelectNew(datePickerTableView: DatePickerTableView) {
        
    }
}